﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using System.Linq.Expressions;
using BitPortal.Utils;

namespace BitPortal.Business
{
    public static partial class ArticleCategoryBLL
    {
        public static int Insert(this ArticleCategory artCat)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            bitEnt.ArticleCategories.AddObject(artCat);
            return bitEnt.SaveChanges();
        }

        public static void DeletePermanently(ArticleCategory cat, ExtendedBitPortalEntities bitEnt)
        {
            foreach (ArticleCategory child in bitEnt.ArticleCategories.Where(ent => ent.ParentID == cat.CategoryID))
            {
                DeletePermanently(child,bitEnt);
            }
            bitEnt.ArticleCategories.DeleteObject(cat);
        }
        /// <summary>
        /// Xoa truc tiep
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public static void DeletePermanently(int categoryID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            ArticleCategory category = bitEnt.ArticleCategories.SingleOrDefault(ac => ac.CategoryID == categoryID);
            if (category != null)
            {
                DeletePermanently(category,bitEnt);
            }
            bitEnt.SaveChanges();
        }

        public static void Delete(ArticleCategory cat, ExtendedBitPortalEntities bitEnt)
        {
            foreach (ArticleCategory child in bitEnt.ArticleCategories.Where(ent => ent.ParentID == cat.CategoryID))
            {
                Delete(child, bitEnt);
            }
            cat.IsDeleted = true;
        }
        /// <summary>
        /// Xoa tam thoi(dat IsDeleted = true)
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public static void Delete(int categoryID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            ArticleCategory category = bitEnt.ArticleCategories.SingleOrDefault(ac => ac.CategoryID == categoryID);
            if (category != null)
            {
                Delete(category, bitEnt);
            }
            bitEnt.SaveChanges();
        }

        /// <summary>
        /// Lay ra danh sach tat ca nhom con chau cua nhom hien tai.
        /// </summary>
        /// <param name="parentArtCate"></param>
        /// <param name="childs"></param>
        public static void GetAllChilds(this ArticleCategory parentArtCate, List<ArticleCategory> childs)
        {
            if (parentArtCate == null)
            {
                foreach (ArticleCategory ac in GetAll().Where(ent => ent.ParentID == null).ToList())
                {
                    childs.Add(ac);
                    GetAllChilds(ac, childs);
                }
            }
            else
            {
                foreach (ArticleCategory ac in GetAll().Where(ent => ent.ParentID == parentArtCate.CategoryID).ToList())
                {
                    childs.Add(ac);
                    GetAllChilds(ac, childs);
                }
            }

        }

        public static List<ArticleCategory> GetAllChilds(this ArticleCategory parentArtCate)
        {
            List<ArticleCategory> childs = new List<ArticleCategory>();
            parentArtCate.GetAllChilds(childs);
            return childs;
        }

        /// <summary>
        /// Lay danh sach nhung nhom cha cua nhom hien tai.
        /// </summary>
        /// <param name="childArtCate"></param>
        /// <returns></returns>
        public static List<ArticleCategory> GetParents(this ArticleCategory childArtCate)
        {
            List<ArticleCategory> listParents = new List<ArticleCategory>();

            while (childArtCate.ParentID != null)
            {
                childArtCate = GetAll().SingleOrDefault(ent => ent.CategoryID == childArtCate.ParentID);
                if (childArtCate == null)
                {
                    break;
                }
                listParents.Add(childArtCate);
            }

            return listParents;
        }

        /// <summary>
        /// Lay ra danh sach nhom con cua nhom hien tai.
        /// </summary>
        /// <param name="parentArtCate"></param>
        /// <returns></returns>
        public static List<ArticleCategory> GetChilds(this ArticleCategory parentArtCate)
        {
            if (parentArtCate == null)
            {
                return GetAll().Where(ac => ac.ParentID == null).ToList();
            }
            return GetAll().Where(ac => ac.ParentID == parentArtCate.CategoryID).ToList();
        }

        /// <summary>
        /// Lay ra danh sach tat ca du lieu
        /// </summary>
        /// <returns></returns>
        public static IQueryable<ArticleCategory> GetAll()
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            var list = from ac in bitEnt.ArticleCategories
                       where ac.IsDeleted == false
                       select ac;
            return list;

        }

        /// <summary>
        /// Lay ra doi tuong theo ID
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public static ArticleCategory GetByID(int categoryID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            return bitEnt.ArticleCategories.SingleOrDefault(ac => ac.CategoryID == categoryID);
        }

        /// <summary>
        ///  Lay ra danh sach du lieu theo dieu kien truyen vao
        /// </summary>
        /// <param name="languageCode"></param>
        /// <param name="isEnabled"></param>
        /// <param name="parentID"></param>
        /// <param name="allowUserPost"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="sort"></param>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static List<ArticleCategory> GetByCondition(string languageCode = null, bool? isEnabled = null, int? parentID = -1, bool? allowUserPost = null, int start = -1, int limit = -1, string sort = null, string dir = null)
        {
            //ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            var list = GetAll().Where(ac => (languageCode == null || ac.LanguageCode.Equals(languageCode)) &&
                (isEnabled == null || ac.IsEnabled == isEnabled) &&
                (parentID == -1 || ac.ParentID == parentID) &&
                (allowUserPost == null || ac.AllowUserPost == allowUserPost)
                );

            //Start Sorting
            if (sort != null)
            {
                var param = Expression.Parameter(typeof(ArticleCategory), "ac");
                var sortExpression = Expression.Lambda<Func<ArticleCategory, object>>(Expression.Property(param, sort), param);
                if (dir == "DESC")
                {
                    list = list.OrderByDescending(sortExpression);
                }
                else
                {
                    list = list.OrderBy(sortExpression);
                }
            }

            //Start Paging
            if (start >= 0 && limit > 0)
            {
                list = list.Skip(start).Take(limit);
            }

            return list.ToList();
        }

        /// <summary>
        /// Lay duong link da Rewrite
        /// </summary>
        /// <param name="cat"></param>
        /// <returns></returns>
        public static string GetSEOUrl(this ArticleCategory cat)
        {
            return "/Article/" + cat.Name.ToSeoString() + "-ac" + cat.CategoryID + ".aspx";
        }

    }
}